package com.xbongbong.pro.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.message.pojo.dto.AutoClueAssignmentRabbitMqDTO;
import com.xbongbong.pro.message.pojo.dto.PublicGroupRabbitMqDTO;
import com.xbongbong.pro.message.pojo.vo.AutoClueAssignmentRabbitMqVO;
import com.xbongbong.pro.message.pojo.vo.PublicGroupRabbitMqVO;
import com.xbongbong.pro.rabbitmq.binders.PublicGroupConsumerBinder;
import com.xbongbong.pro.rabbitmq.constant.AutoClueAssignmentBinderConstant;
import com.xbongbong.pro.rabbitmq.constant.PublicGroupBinderConstant;
import com.xbongbong.pro.rabbitmq.feign.AutoClueAssignmentFeignClient;
import com.xbongbong.pro.rabbitmq.feign.PublicGroupFeignClient;
import feign.RetryableException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.context.i18n.LocaleContextHolder;

import javax.annotation.Resource;
import java.util.Locale;

/**
 * @author long
 * @version v1.0
 * @date 2021/1/18 10:59
 * @since v1.0
 */
@EnableBinding(PublicGroupConsumerBinder.class)
public class PublicGroupListener {

    private static final Logger logger = LoggerFactory.getLogger(PublicGroupListener.class);

    @Resource
    private PublicGroupFeignClient publicGroupFeignClient;

    @StreamListener(value = PublicGroupBinderConstant.PUBLIC_GROUP_INPUT)
    public void bindPublicGroup(PublicGroupRabbitMqDTO publicGroupRabbitMqDTO) {
        XbbResponse<PublicGroupRabbitMqVO> response;
        try {
            Locale locale = LocaleContextHolder.getLocale();
            String lang = locale.toString();
            response = publicGroupFeignClient.bindPublicGroup(publicGroupRabbitMqDTO, lang);
            logger.info("bindPublicGroup message=" + JSON.toJSONString(publicGroupRabbitMqDTO) + "\n response=" + JSON.toJSONString(response));
        } catch (RetryableException e) {
            logger.error("PublicGroupListener.bindPublicGroup RetryableException case by Read timed out");
        } catch (Exception e) {
            logger.error("PublicGroupListener.bindPublicGroup Exception", e);
        }

    }
}
